package com.gd.data.platform.mapper;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gd.data.platform.entity.RealtimeMetrics;

/**
 * 实时指标Mapper
 * 操作ClickHouse DWS层中的实时指标数据
 * 
 * @author gd-data-platform
 * @since 1.0.0
 */
@Mapper
public interface RealtimeMetricsMapper extends BaseMapper<RealtimeMetrics> {

    /**
     * 获取最新的实时指标
     */
    @Select("SELECT * FROM realtime_metrics " +
            "WHERE create_time >= #{startTime} " +
            "ORDER BY create_time DESC " +
            "LIMIT #{limit}")
    List<RealtimeMetrics> getLatestMetrics(@Param("startTime") LocalDateTime startTime,
            @Param("limit") Integer limit);

    /**
     * 根据指标类型获取最新指标
     */
    @Select("SELECT * FROM realtime_metrics " +
            "WHERE metric_type = #{metricType} AND create_time >= #{startTime} " +
            "ORDER BY create_time DESC " +
            "LIMIT #{limit}")
    List<RealtimeMetrics> getLatestMetricsByType(@Param("metricType") String metricType,
            @Param("startTime") LocalDateTime startTime,
            @Param("limit") Integer limit);

    /**
     * 获取指定时间窗口内的指标趋势
     */
    @Select("SELECT metric_type, AVG(metric_value) as avg_value, " +
            "MIN(metric_value) as min_value, MAX(metric_value) as max_value " +
            "FROM realtime_metrics " +
            "WHERE create_time >= #{startTime} AND create_time <= #{endTime} " +
            "GROUP BY metric_type")
    List<Map<String, Object>> getMetricsTrend(@Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime);

    /**
     * 获取告警指标（超过阈值的指标）
     */
    @Select("SELECT * FROM realtime_metrics " +
            "WHERE metric_type = #{metricType} AND metric_value > #{threshold} " +
            "AND create_time >= #{startTime} " +
            "ORDER BY create_time DESC")
    List<RealtimeMetrics> getAlertMetrics(@Param("metricType") String metricType,
            @Param("threshold") Double threshold,
            @Param("startTime") LocalDateTime startTime);
}
